package view.panel.mail
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.text.TextField;
	
	import global.Resource;
	
	import utils.DisplayTool;
	import utils.TextTool;
	
	import view.components.RemoteHtmlImg;
	import view.components.list.ITileListItem;

	public class MailItem extends Sprite implements ITileListItem
	{
		private var _selected:Boolean;
		protected var nameText:TextField;
		protected var selectSign:Bitmap;
		private var remoteImg:RemoteHtmlImg;
		private var _data:Object;
		protected var _imgWidth:int;
		protected var _imgHeight:int;
		
		public function MailItem()
		{
			this.mouseChildren = false;
			init();
		}
		
		public function switchState(state:int):void
		{}
		
		private function init():void
		{
			nameText = TextTool.getTextField(130,20,12,0xFFFFFF);
			nameText.x = 55;
			nameText.y = 18;
			addChild(nameText);
			
			remoteImg = new RemoteHtmlImg(50,50);
			remoteImg.x = 2;
			addChild(remoteImg);
			
			selectSign = Resource.getImg("face_select");
			selectSign.x = 135;
			selectSign.y = nameText.y + 2;
			addChild(selectSign);
			selectSign.visible = false;
		}
		
		public function get self():DisplayObject
		{
			return this;
		}
		
		public function update(data:Object):void
		{
			nameText.text = data.name;
			remoteImg.loadImg(data.image);
			_data = data;
		}
		
		public function drawSelectBg():void
		{
			with(this.graphics)
			{
				clear();
				beginFill(0xFFFFFF,.6);
				drawRect(0,0,width,height);
				endFill();
			}
			selectSign.visible = true;
			data.result = 1;
		}
		
		public function drawOverBg():void
		{
			with(this.graphics)
			{
				clear();
				if (_selected)
				{
					beginFill(0xFFFFFF,.6);
				}
				else
				{
					beginFill(0xFFFFFF,.3);
				}
				
				drawRect(0,0,width,height);
				endFill();
			}
			selectSign.visible = _selected;
			data.result = int(_selected);
		}
		
		public function drawOutBg():void
		{
			if (_selected)
			{
				with(this.graphics)
				{
					clear();
					beginFill(0xFFFFFF,.4);
					drawRect(0,0,width,height);
					endFill();
				}
			}
			else
			{
				this.graphics.clear();
			}
			selectSign.visible = _selected;
			data.result = int(_selected);
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function set selected(v:Boolean):void
		{
			_selected = v;
		}
		
		public function destroy():void
		{
			remoteImg.destroy();
			nameText = null;
			DisplayTool.removeAllChild(this);
		}
		
		public function getInstance():ITileListItem
		{
			return new MailItem();
		}
		
		public function get data():Object
		{
			return _data;
		}
		
		private var _index:int = -1;
		
		public function get index():int
		{
			return _index;
		}
		
		public function set index(v:int):void
		{
			_index = v;
		}
	}
}